<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" v-show="showSearch" :inline="true">
      <el-form-item label="供应商" prop="supId">
        <el-select
          v-model="queryParams.supId"
          placeholder="供应商"
          clearable
          size="small"
          style="width: 240px"
        >
          <el-option
            v-for="dict in supplierList"
            :key="dict.supId"
            :label="dict.supName"
            :value="dict.supId"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="申请人" prop="createBy">
        <el-input
          v-model="queryParams.createBy"
          placeholder="申请人"
          clearable
          size="small"
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="单据状态" prop="purVerify">
        <el-select
          v-model="queryParams.purVerify"
          placeholder="单据状态"
          clearable
          size="small"
          style="width: 240px"
        >
          <el-option
            v-for="dict in purchaseStatusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>


      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:exam_cost:add']"
        >审核通过</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-delete"
          size="mini"
          @click="handleUpdate"
          v-hasPermi="['system:exam_cost:edit']"
        >审核不通过</el-button>
      </el-col>



      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="purchaseList" @selection-change="handleSelectionChange" border="0px">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="单据ID" align="center" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <router-link :to="'/drug/purchase/date/' + scope.row.purOrderId" class="link-type">
            <span>{{ scope.row.purOrderId }}</span>
          </router-link>
        </template>
      </el-table-column>
      <el-table-column label="供应商" align="center" key="supplier.supName" prop="supplier.supName" :show-overflow-tooltip="true"  />
      <el-table-column label="采购批发总额" align="center" key="purCount"  >
        <template slot-scope="scope">
        <span v-text="scope.row.purCount==null?0:scope.row.purCount"></span>
        </template>
      </el-table-column>
      <el-table-column  label="状态" align="center" key="purVerify" prop="purVerify" :formatter="purVerifyFormat" style="width: 20px" ></el-table-column>
      <el-table-column label="申请人" align="center" key="createBy"  prop="createBy"  />
      <el-table-column label="入库操作人" align="center" key="updateBy"  prop="updateBy"  />
      <el-table-column label="入库时间" align="center" prop="updateTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />




  </div>
</template>

<script>
//import { listRole, getRole, delRole, addRole, updateRole, exportRole, dataScope, changeRoleStatus } from "@/api/system/role";
import { listPurchase, getSupplier , addExam_cost  , updateExam_cost , deletePur , Ruchange , putNoCheck , putOkCheck} from "@/api/purchase/check"
import { treeselect as menuTreeselect, roleMenuTreeselect } from "@/api/system/menu";
import { treeselect as deptTreeselect, roleDeptTreeselect } from "@/api/system/dept";

export default {
  name: "Purchase",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      purOrderIds: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 订单表格数据
      purchaseList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否显示弹出层（数据权限）
      openDataScope: false,
      menuExpand: false,
      menuNodeAll: false,
      deptExpand: true,
      deptNodeAll: false,
      // 日期范围
      dateRange: [],
      // 供应商列表
      supplierList:[],
      //订单状态列表
      purchaseStatusOptions: [],
      // 数据范围选项
      dataScopeOptions: [
        {
          value: "1",
          label: "全部数据权限"
        },
        {
          value: "2",
          label: "自定数据权限"
        },
        {
          value: "3",
          label: "本部门数据权限"
        },
        {
          value: "4",
          label: "本部门及以下数据权限"
        },
        {
          value: "5",
          label: "仅本人数据权限"
        }
      ],
      // 菜单列表
      menuOptions: [],
      // 部门列表
      deptOptions: [],

      purChases:[],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        purVerify: undefined,
        createBy : undefined,
        supId:undefined
      },
      // 表单参数
      form: {},

      // 表单校验
      depts: {
        DeptsName: [
          { required: true, message: "角色名称不能为空", trigger: "blur" }
        ],
        status: [
          { required: true, message: "权限字符不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
    this.getSupplierList();
    this.getDicts("purchase_status").then(response => {
      this.purchaseStatusOptions = response.data;
    });
  },
  methods: {
    /** 查询检查项目列表 */
    getList() {
      this.loading = true;
      this.purchaseList = []
      listPurchase(this.addDateRange(this.queryParams, this.dateRange)).then(
        response => {
          for (var i = 0 ; i < response.rows.length ; i ++){
            if (response.rows[i].purVerify==1){
              this.purchaseList.push(response.rows[i])
            }
          }
          this.total = response.total;
          this.loading = false;
        }
      );
    },

    getSupplierList() {
      this.loading = true;
      getSupplier().then(
        response => {
          this.supplierList = response.data;
        }
      );
    },


    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 取消按钮（数据权限）
    cancelDataScope() {
      this.openDataScope = false;
      this.reset();
    },
    // 表单重置
    reset() {
      if (this.$refs.menu != undefined) {
        this.$refs.menu.setCheckedKeys([]);
      }
      this.menuExpand = false,
      this.menuNodeAll = false,
      this.deptExpand = true,
      this.deptNodeAll = false,
      this.form = {
        examId: undefined,
        examName: undefined,
        examKey: undefined,
        examOnePrice: undefined,
        examCost: undefined,
        examUnit: undefined,
        status: "0",
        deptIds: [],
        menuCheckStrictly: true,
        deptCheckStrictly: true,
        remark: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.purChases=selection.map(item => item)
      for (var i = 0 ; i < this.purChases.length ; i++){
        if (this.purOrderIds.indexOf(this.purChases[i].purOrderId)==-1) {
          this.purOrderIds.push(this.purChases[i].purOrderId)
        }
      }

      this.single = selection.length!=1
      this.multiple = !selection.length
    },

    /** 新增按钮操作 */
    handleAdd() {
      const purOrderIds = this.purOrderIds
      putNoCheck(purOrderIds).then(response => {
        this.getList();
      });
    },

    /** 提交审核按钮操作 */
    handleUpdate(row) {
      const purOrderIds = this.purOrderIds
      putOkCheck(purOrderIds).then(response => {
        this.getList();
      });
    },


    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.examId != undefined) {
            updateExam_cost(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addExam_cost(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },

    /** 删除作废按钮操作 */
    handleDelete(row) {
      const purOrderIds = this.purOrderIds
      deletePur(purOrderIds).then(response => {
        this.getList();
      });
    },

    RuChangeStatus(row) {
      var b = confirm("是否提交" + this.purOrderIds + "订单入库")
      if (b) {
        Ruchange(this.purOrderIds).then(response => {
          this.getList();
        });
      }

    },
    // 菜单状态字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },

    // 菜单状态字典翻译
    purVerifyFormat(row, column) {
      return this.selectDictLabel(this.purchaseStatusOptions, row.purVerify);
    }

    },



};
</script>
